#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <set>

#define flr(x, l, r) for (int x = l; x <= r; ++x)
#define frl(x, r, l) for (int x = r; x >= l; --x)
#define fmp(x, t) for (int x = h[t]; ~x; x = ne[x])
#define LL long long
#define mt memset
#define my memcpy
#define szf sizeof
#define INF 0x3f3f3f3f
#define in(x) scanf("%d", &x)
#define out(x) printf("%d", x)
#define inll(x) scanf("%lld", &x)
#define outll(x) printf("%lld", x)
#define pn printf("\n")
#define con continue
#define bk break
#define vc vector
#define pb push_back
#define sz size
#define PII pair<int, int>
#define x first
#define y second
#define P_q priority_queue
#define ft front
#define pf push_front
#define popf pop_front
#define it insert
#define ct count

using namespace std;

const int N = 30;

int n, m;
bool d[N][N], st[N];

void floyd() {
    flr (k, 0, n - 1)
        flr (i, 0, n - 1)
            flr (j, 0, n - 1)
                d[i][j] |= d[i][k] && d[k][j];
}

int check() {
    flr (i, 0, n - 1)
            if (d[i][i]) 
                return 2;
    
    flr (i, 0, n - 1)
        flr(j, 0, i - 1)
            if (!d[i][j] && !d[j][i]) 
                return 0;
    
    return 1;
}

char get_min() {
    flr (i, 0, n - 1)
        if (!st[i]) {
            bool flag = true;
            
            flr (j, 0, n - 1)
                if (!st[j] && d[j][i])
                    flag = false;
            
            if (flag) {
                st[i] = true;
                return 'A' + i;
            }
        }
}

int main() {
	while (cin >> n >> m, n || m) {
	    mt(d, 0, szf d);
	    int type = 0, t;
	    flr (i, 1, m) {
    	    char str[5];
    	    scanf("%s", str);
    	    int a = str[0] - 'A', b = str[2] - 'A';
    	    
    	    if (!type) {
    	        d[a][b] = 1;
    	        floyd();
    	        type = check();
    	        if (type) t = i;
    	    }
	    }
	    
	    if (!type) puts("Sorted sequence cannot be determined.");
	    else if (type == 2)
	        printf("Inconsistency found after %d relations.\n", t);
	    else {
	        mt(st, 0, szf st);
	        printf("Sorted sequence determined after %d relations: ", t);
	        flr (i, 0, n - 1) printf("%c", get_min());
	        printf(".\n");
	    }
	}
    
    return 0;
}